package com.clayecloud.common.client.controller;

import com.clayecloud.common.annotation.NoAuthorization;
import com.clayecloud.common.client.entity.ClientUser;
import com.clayecloud.common.client.services.UserServices;
import com.clayecloud.common.entity.User;
import com.clayecloud.common.utils.JJWTUtil;
import com.clayecloud.common.utils.MessageUtil;
import com.clayecloud.common.utils.StringUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/login")
@Api(tags = "登录接口")
public class ClientLoginController {

    @Resource
    private UserServices userServices;

    @GetMapping("/")
    @ApiOperation("登录")
    @NoAuthorization
    public MessageUtil login(String account, String userPassword){
        if (account == null || userPassword == null){
            return MessageUtil.fail("登录失败(账号/密码不能为空)");
        }
        //进行email登录
        if (StringUtil.isEmail(account)){
            MessageUtil messageUtil = userServices.selectByMail(account);
            return  getLoginMessageUtil(messageUtil, userPassword);
        }
        //进行phone登录
        if (StringUtil.isPhone(account)){
            MessageUtil messageUtil = userServices.selectByPhone(account);
            return  getLoginMessageUtil(messageUtil, userPassword);
        }
        return MessageUtil.fail("登录失败(未注册)");
    }

    /**
     * 获取用户信息
     * @return
     */
    @GetMapping("/userinfo")
    @ApiOperation("获取个人信息")
    public MessageUtil getUserInfo(String token){
        if(token == null){
            return MessageUtil.fail("token不能为空");
        }
        if(!JJWTUtil.verifyToken(token)){
            return MessageUtil.fail("请重新登录");
        }
        Claims claims= JJWTUtil.getClaims(token);
        MessageUtil messageUtil = userServices.selectByPrimaryKey(Integer.parseInt(claims.get("userID").toString()));
        User user = (User) messageUtil.getObj();
        return MessageUtil.success("获取用户基础信息成功").add(new ClientUser(user.getUserId(),user.getUserName(),user.getUserState(),user.getUserInfo(),user.getUserAvatar()));
    }


    /**
     * 登录条件判断
     * @param messageUtil
     * @param userPassword
     * @return
     */
    private MessageUtil getLoginMessageUtil(MessageUtil messageUtil, String userPassword){
        if (messageUtil.getObj() == null){
            return MessageUtil.fail("登录失败(未注册)");
        }
        User user = (User) messageUtil.getObj();
        if (user.getUserPassword().equals(userPassword)){
            Map claims = new HashMap<>();
            claims.put("userID",user.getUserId().toString());
            return MessageUtil.success("登录成功").add(JJWTUtil.createToken(claims, 24L*3600*1000*30));
        }else{
            return MessageUtil.fail("登录失败(密码错误)");
        }
    }

}
